package com.ruoyi.riskrule.service.impl;


import com.ruoyi.bizsys.domain.CustomerCrsReport;
import com.ruoyi.bizsys.domain.CustomerXdsReport;
import com.ruoyi.bizsys.domain.RealNameAuth;
import com.ruoyi.bizsys.service.ICustomerCrsReportService;
import com.ruoyi.bizsys.service.ICustomerXdsReportService;
import com.ruoyi.bizsys.service.IRealNameAuthService;
import com.ruoyi.bizsys.utils.BizConstants;
import com.ruoyi.common.constant.RiskConstants;
import com.ruoyi.common.utils.Func;
import com.ruoyi.extdata.service.ICrsService;
import com.ruoyi.extdata.service.IXdsService;
import com.ruoyi.riskrule.domain.RuleCreditDto;
import com.ruoyi.riskrule.domain.RuleNextDto;
import com.ruoyi.riskrule.service.ICustomerCreditService;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.*;

/**
 * @author a
 */
@Slf4j
@Service
@AllArgsConstructor
public class CustomerCreditServiceImpl implements ICustomerCreditService {

    private IXdsService xdsService;

    private ICrsService crsService;

    private ICustomerXdsReportService customerXdsReportService;

    private ICustomerCrsReportService customerCrsReportService;

    private IRealNameAuthService realNameAuthService;

    /**
     * @param userId
     * @param needXds 1:优先调用接口 0:查询本地
     * @return
     */
    @Override
    public RuleNextDto getConsumerCreditData(String userId, String needXds,String applyType) {

        RuleNextDto ruleNextDTO = new RuleNextDto();
        ruleNextDTO.setReportChecked("-1" );

        //判断xds crs 是否存在本地数据 是否超出30天有效期
        RealNameAuth realNameAuth = buildConsumerMatch(userId);
        RuleCreditDto ruleCredit = creditCheck(userId,realNameAuth.getBvnAccount());

        boolean isSelNew;
        if (RiskConstants.RULE_TYPE_LOAN.equals(applyType)){
            //1:提现时风控指定查询最新数据 ｜ 不存在本地数据
            isSelNew = "1".equals(needXds) || ruleCredit.getLocalReport() == null;
        }else {
            // 不存在本地数据 | 数据超出30天无效
            isSelNew = ruleCredit.getLocalReport() == null || Func.dayDifference(ruleCredit.getCreateTime()) >= 30;
        }

        if (isSelNew) {
            Object consumerFullCreditReport = null;
            try {
                //接口查询
                log.info("获取CRS报告" );
                consumerFullCreditReport = crsService.getConsumerFullCreditReport(realNameAuth.getUserId(), realNameAuth.getBvnAccount());
                if (consumerFullCreditReport == null) {
                    log.info("获取XDS报告" );
                    consumerFullCreditReport = xdsService.getConsumerFullCreditReport(realNameAuth.getUserId(), realNameAuth.getBvnAccount(), realNameAuth.getAccountNumber());
                }
            } catch (Exception e) {
                log.error("get report error, continue", e);
                consumerFullCreditReport = null;
            }
            //未查到结果则默认使用库数据库
            if (consumerFullCreditReport != null) {
                ruleNextDTO.setReportChecked("1" );
                ruleNextDTO.setCreditReport(consumerFullCreditReport);
                return ruleNextDTO;
            }
        }
        //本地数据存在且有效
        if (Func.isNotEmpty(ruleCredit.getLocalReport()) && "1".equals(ruleCredit.getReportStatus())) {
            ruleNextDTO.setReportChecked("0" );
            ruleNextDTO.setCreditReport(ruleCredit.getLocalReport());
            return ruleNextDTO;
        }
        return ruleNextDTO;
    }

    @Override
    public RuleCreditDto creditCheck(String userId,String bvn) {

        RuleCreditDto ruleCreditDto  = new RuleCreditDto();

        CustomerCrsReport customerCrsReport = new CustomerCrsReport();
        customerCrsReport.setBvn(bvn);
        customerCrsReport.setStatus("1" );
        List<CustomerCrsReport> customerCrsReports = customerCrsReportService.selectCustomerCrsReportList(customerCrsReport);

        CustomerXdsReport customerXdsReport = new CustomerXdsReport();
        customerXdsReport.setUserId(userId);
        customerXdsReport.setStatus("1" );
        List<CustomerXdsReport> customerXdsReports = customerXdsReportService.selectCustomerXdsReportList(customerXdsReport);

        if (customerCrsReports.size() > 0) {
            ruleCreditDto.setLocalReport(customerCrsReports.get(0));
            ruleCreditDto.setCreateTime(customerCrsReports.get(0).getCreateTime());
            ruleCreditDto.setReportStatus(customerCrsReports.get(0).getStatus());
        }
        if (customerXdsReports.size() > 0) {
            ruleCreditDto.setLocalReport(customerXdsReports.get(0));
            ruleCreditDto.setCreateTime(customerXdsReports.get(0).getCreateTime());
            ruleCreditDto.setReportStatus(customerXdsReports.get(0).getStatus());
        }
        if (customerCrsReports.size() > 0 && customerXdsReports.size() > 0) {
            int i1 = customerCrsReports.get(0).getCreateTime().compareTo(customerXdsReports.get(0).getCreateTime());
            if (i1 > 0) {
                ruleCreditDto.setLocalReport(customerCrsReports.get(0));
                ruleCreditDto.setCreateTime(customerCrsReports.get(0).getCreateTime());
                ruleCreditDto.setReportStatus(customerCrsReports.get(0).getStatus());
            } else {
                ruleCreditDto.setLocalReport(customerXdsReports.get(0));
                ruleCreditDto.setCreateTime(customerXdsReports.get(0).getCreateTime());
                ruleCreditDto.setReportStatus(customerXdsReports.get(0).getStatus());
            }
        }

        return ruleCreditDto;
    }

    @Override
    public String geXdsTime(String userId) {

        CustomerXdsReport customerXdsReport = new CustomerXdsReport();
        customerXdsReport.setUserId(userId);
        customerXdsReport.setStatus("1" );
        List<CustomerXdsReport> customerXdsReports = customerXdsReportService.selectCustomerXdsReportList(customerXdsReport);
        if (customerXdsReports.size() == 0) {
            return "2000-01-01 00:00:00";
        }
        return new SimpleDateFormat("yyyy-MM-dd HH:mm:ss" ).format(customerXdsReports.get(0).getCreateTime());
    }


    private RealNameAuth buildConsumerMatch(String userId) {
        RealNameAuth realNameAuthP = new RealNameAuth();
        realNameAuthP.setUserId(userId);
        return realNameAuthService.selectRealNameAuthList(realNameAuthP).get(0);
    }


}
